import processing.core.*; 
import processing.xml.*; 

public class libProIm{

public PImage img;
public int rgb;

public libProIm(PImage image){

  this.img=image;

  int i,j;
  i=image.width;
  j=image.height;
  int[][]x=new int[i][j];

  int[]RGBCode=new int[i+j];
  int[]count=new int[i+j];

  for(int k=0;k<i;k++){
    for(int l=0;l<j;l++){
      x[k][l]=image.get(k,l);
      insertRGB(count,RGBCode,x[k][l]);
    }
  }

  int maxColor = MaxColor(count,RGBCode);

  img.loadPixels();
  for (int k = 0; k< img.pixels.length;k++) {
    img.pixels[k] = maxColor; 
  }
  img.updatePixels();
  
  this.rgb=maxColor;
  this.img=img;
}

public void insertRGB(int[]rep,int[]rgb,int code){
  for (int i=0;i<rgb.length;i++){
      if(rgb[i]==code){rep[i]=rep[i]+1;break;}
      if(rgb[i]==0){rgb[i]=code;rep[i]=rep[i]+1;break;}
  }
}

public int MaxColor(int[]c, int []code){
  int maxi=0;
  for(int i=0;i<c.length;i++){
    if(maxi<c[i]){maxi=i;}
    if(c[i]==0){break;}
    }
  return code[maxi];
  }

public PImage getImageColor(){
	PImage p= this.img.get();
	p.loadPixels();
	for(int i=0;i<this.img.pixels.length; i++){p.pixels[i]=this.rgb;}
	p.updatePixels();
	return p;
}

}
